import pymongo
from config.configs import ALPHA_DB_HOST, ALPHA_DB_PORT
from tools.funciton import spuId_to_articleNumber, skuId_to_size, write_xlsx

alpha = pymongo.MongoClient(ALPHA_DB_HOST, ALPHA_DB_PORT).alpha


def alpha_settlement_export():
    """集物结算导出"""
    pipeline = [
        {
            '$match':
                {
                    'merchantCode': "0000"
                }
        },
        {
            '$project':
                {
                    'id': {"$toString": "$_id"},
                    'article_number': 1,
                    'props': 1,
                    'order_no': 1,
                    'order_created_time': 1,
                    'real_stmt_time': 1,
                    'merchantCode': 1,
                    'sku_price': 1,
                    'sum_plate_fee': 1,
                    'transfer_fee': 1,
                    'operation_fee': 1,
                    'consumer_freight_subsidy_fee': 1,
                    'stmt_fee': 1,
                    'warehouseOperationFee': 1,
                    'warehouseTechnicalFee': 1,
                    'merchantStmtFee': 1,
                    'warehouseTechnicalFeeRate': 1,
                    'settlement_channel': 1,
                    'amount_receivable': 1,
                    '_id': 0
                }
        }
    ]
    li = []
    for i in alpha.dewustatements.aggregate(pipeline=pipeline):
        if 'amount_receivable' or 'real_stmt_time' not in i:
            li.append(
                [i['order_no'], i['id'], i['article_number'], i['props'], i['order_created_time'], '', i['sku_price'],
                 i['sum_plate_fee']
                    , i['transfer_fee'], i['operation_fee'], i['stmt_fee'], i['warehouseOperationFee'],
                 i['warehouseTechnicalFee'],
                 i['merchantStmtFee'], i['warehouseTechnicalFeeRate'], i['settlement_channel'], '', i['merchantCode']])
        else:
            li.append(
                [i['order_no'], i['id'], i['article_number'], i['props'], i['order_created_time'], i['real_stmt_time'],
                 i['sku_price'], i['sum_plate_fee']
                    , i['transfer_fee'], i['operation_fee'], i['stmt_fee'], i['warehouseOperationFee'],
                 i['warehouseTechnicalFee'],
                 i['merchantStmtFee'], i['warehouseTechnicalFeeRate'], i['settlement_channel'],
                 i['amount_receivable'], i['merchantCode']])
    write_xlsx(['得物订单号', '集物订单号', '货号', '尺码', '订单创建时间', '得物结算时间', '商品价格', '得物服务费',
                '得物转账手续费', '得物技术服务费', '得物结算金额', '集物仓库操作费', '集物技术服务费',
                '集物商家结算金额',
                '集物技术服务费率', '结算渠道', '实付价格', '商家编号'], li, '0000结算导出')

# alpha_settlement_export()
